﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.ModelInterface;
using QLBH.Model.Controller;
using QLBH.DataModel;
using QLBH.Base;

namespace QLBH.FeatureModel
{
    public class HDBanHangModel:IHDBanHang
    {
        public Guid HDBHID
        {
            get; 
            set;
        }

        public string SOHD
        {
            get;
            set;
        }

        public DateTime NGAYTAO
        {
            get;
            set;
        }

        public double THUE
        {
            get;
            set;
        }

        public decimal CHIETKHAU
        {
            get;
            set;
        }

        public decimal TONGTIEN
        {
            get;
            set;
        }

        public string GHICHU
        {
            get;
            set;
        }

        public Guid USERID
        {
            get;
            set;
        }

        public string USERNAME
        {
            get;
            set;
        }

        public bool BANLE
        {
            get;
            set;
        }
        public Guid KHACHHANGID { set; get; }


        public string KHTEN { set; get; }
        
        public HDBanHangModel()
        {
            HDBHID = Guid.NewGuid();
            NGAYTAO = DateTime.Now;
            SOHD = CodeHelper.GetNewCode();
            mathang = new List<IHD_MatHangModel>();

            USERID = AccountHandler.GetCurrentUserLogin().NVID;
            USERNAME = AccountHandler.GetCurrentUserLogin().TENNV;

            BANLE = true;

            KHACHHANG khachhang=KhachHangController.GetInstance().FindBy(kh => kh.KHCODE=="KL").FirstOrDefault();
            if (khachhang!=null)
            {
                KHACHHANGID = khachhang.KHID;
                KHTEN = khachhang.TENKH;
            }                                       
        }

        public void InitFromEntity(DataModel.HOADONBANHANG entity)
        {
            this.BANLE = (bool)entity.BANLE;
            this.CHIETKHAU = (decimal)entity.CHIETKHAU;
            this.GHICHU = entity.GHICHU;
            this.HDBHID = entity.HDBHID;
            this.NGAYTAO = (DateTime)entity.NGAYTAO;
            this.SOHD = entity.SOHD;
            this.THUE = (double)entity.THUE;
            this.TONGTIEN = (decimal)entity.TONGTIEN;
            this.USERID = (Guid)entity.NGUOITAOID;

            USERID = AccountHandler.GetCurrentUserLogin().NVID;
            USERNAME = AccountHandler.GetCurrentUserLogin().TENNV;

            KHACHHANGID = entity.KHACHHANGID;

            KHACHHANG khachhang = KhachHangController.GetInstance().FindBy(kh => kh.KHID == KHACHHANGID).FirstOrDefault();
            KHTEN = khachhang.TENKH;

            //Init mathang

            List<HDBHCHITIET> bhcts=HDBanHangChiTietController.GetInstance().FindBy(hd => hd.HDBHID==HDBHID).ToList();

            foreach (HDBHCHITIET bhct in bhcts)
            {
                IHD_MatHangModel modelItem = new HD_MatHangModel();
                modelItem.InitFromEntity(bhct);

                mathang.Add(modelItem);
            }                          
        }

        public DataModel.HOADONBANHANG ToEntity()
        {
            HOADONBANHANG ENTITY = new HOADONBANHANG();
            ENTITY.BANLE = BANLE;
            ENTITY.CHIETKHAU = CHIETKHAU;
            ENTITY.GHICHU = GHICHU;
            ENTITY.HDBHID = HDBHID;
            ENTITY.NGAYTAO = NGAYTAO;
            ENTITY.NGUOITAOID = USERID;
            ENTITY.SOHD = SOHD;
            ENTITY.THUE = THUE;
            ENTITY.TONGTIEN = TONGTIEN;

            ENTITY.KHACHHANGID = KHACHHANGID;
            
            foreach (IHD_MatHangModel ctmodel in mathang)
            {
                HDBHCHITIET bhct = ctmodel.ToEntity();

                bhct.HDBHID = HDBHID;

                ENTITY.HDBHCHITIETs.Add(bhct);
            }

            return ENTITY;
        }

        private IList<IHD_MatHangModel> mathang;
        public IList<IHD_MatHangModel> MatHang
        {
            get
            {
                return mathang;
            }
            set
            {
                mathang = value;
            }
        }
    }
}
